﻿using ApplicationModels.A03.Photographs;
using Commons.Helpers;
using Commons.Requests;
using Commons.Responses;
using EntityModels.E03.Photographs;
using IApplicationServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq.Expressions;

namespace WebAPI.Controllers
{

    [Route("[controller]/[action]")]
    public class PhotographController : BaseController<Guid, Photograph, PhotographAM>
    {
        private readonly IPhotographService apiService;
        private readonly RedisHelper redisHelper;
        public PhotographController(IPhotographService apiService) : base(apiService)
        {
            this.apiService = apiService;
            redisHelper = new RedisHelper();
        }

        public override async Task<MessageResponse> AddAsync(PhotographAM apiEntity)
        {
            apiEntity.AppUserId = AppUserId;
            apiEntity.FilesIds = StringHelper.SubStringComma(apiEntity.FilesIds);
            apiEntity.FileShowId = apiEntity.FilesIds.Split(",")[0];
            apiEntity.Tags = StringHelper.SubStringComma(apiEntity.Tags);
            return await apiService.AddAsync(apiEntity);
        }
        public override async Task<MessageResponse> UpdateAsync(PhotographAM apiEntity)
        {
            apiEntity.AppUserId = AppUserId;
            return await base.UpdateAsync(apiEntity);
        }
        /// <summary>
        /// 作品名称、作者、标签
        /// 类别
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        //[AllowAnonymous]
        public override async Task<DatasResponse<PhotographAM>> GetBySelectAsync([FromQuery] PagingRequest req)
        {
            Expression<Func<Photograph, bool>> filter = x => true;
            int searchValueId = req.SearchValue != "0" ? int.Parse(req.SearchValue) : 0;
            if (searchValueId > 0)
            {
                filter = filter.And(x => x.PhotographType.Id == searchValueId);
            }
            if (!string.IsNullOrWhiteSpace(req.SearchName))
            {
                var nameList = req.SearchName.Split(" ");

                // 初始化条件，从第一个关键词开始
                filter = filter.And(x =>
                    x.Name.Contains(nameList[0]) ||
                    x.AppUser.NickName.Contains(nameList[0]) ||
                    x.Tags.Contains(nameList[0]));

                // 对于其他关键词，使用 AND 来确保所有关键词都出现
                if (nameList.Length > 1)
                {
                    for (int i = 1; i < nameList.Length; i++)
                    {
                        var name = nameList[i];
                        filter = filter.Or(x =>
                            x.Name.Contains(name) ||
                            x.AppUser.NickName.Contains(name) ||
                            x.Tags.Contains(name)
                        );
                    }
                }
            }
            if (!string.IsNullOrWhiteSpace(req.ExtendedValue5))
            {
                return await apiService.GetPhotographsWithLikeStatusAsync(req.PageIndex, req.PageSize, filter,Guid.Parse(AppUserId));
            }
            return await ApiService.GetBySelectAsync(req.PageIndex, req.PageSize, filter);
        }

        [HttpGet]
        public async Task<DatasResponse<PhotographAM>> GetByMyselfAsync()
        {
            return await ApiService.GetAllAsync(x => x.AppUserId == Guid.Parse(AppUserId));
        }

        [HttpGet]
        public async Task<DataResponse<PhotographAM>> GetPhotographWithLikeStatusByIdAsync(string id)
        {
            return await apiService.GetPhotographWithLikeStatusAsync(Guid.Parse(id), Guid.Parse(AppUserId));
        }
        /// <summary>
        /// 获取用户作品
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<DatasResponse<PhotographAM>> GetPhotographByUserIdAsync(string? userId)
        {
            return await apiService.GetBySelectAsync(1, 1000, x => x.AppUserId == Guid.Parse(userId));
        }
        /// <summary>
        /// 获取关注用户的作品
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public async Task<DatasResponse<PhotographAM>> GetPhotographByFollow()
        {
            return await apiService.GetPhotographsWithLikeStatusByFollowAsync(Guid.Parse(AppUserId));
        }
        /// <summary>
        /// 临时的增加作品浏览量
        /// </summary>
        /// <param name="photographId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task AddHistory(string photographId)
        {
            await apiService.AddHistory(photographId);
        }
    }
}
